import javax.swing.*;
import java.awt.*;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

public class MysqlFrame extends JInternalFrame {
    public static final String NAME = "MysqlInnerFrame";
    
    public MysqlFrame(JDesktopPane desktopPane) {
        setName(NAME);
        setTitle("Mysql配置");
        setBounds(desktopPane.getWidth() / 5, desktopPane.getHeight() / 5, 3 * desktopPane.getWidth() / 5, 3 * desktopPane.getHeight() / 5);
        setIconifiable(true);
        setMaximizable(false);
        setClosable(true);
        setLayout(null);
        
        Box contentPane = Box.createVerticalBox();
//        contentPane.setBorder(BorderFactory.createLineBorder(Color.RED));
        contentPane.setAlignmentX(Component.CENTER_ALIGNMENT);
        contentPane.setBounds(0, this.getHeight() / 4, this.getWidth(), this.getHeight() / 2);
        
        Map<String, Object> textFieldPropMap = new HashMap<>();
        textFieldPropMap.put("columns", 15);
        Map<String, Object> labelPropMap = new HashMap<>();
        
        JPanel subPane1 = new JPanel();
        subPane1.add(UiBuilder.buildSwingComponent("MysqlFrame.hostLabel", "JLabel", "<html><div style='width:30px; text-align:center;'>主机名</div></html>", labelPropMap), BorderLayout.WEST);
        subPane1.add(UiBuilder.buildSwingComponent("MysqlFrame.hostField", "JTextField", textFieldPropMap), BorderLayout.EAST);
        contentPane.add(subPane1);
        
        JPanel subPane2 = new JPanel();
        subPane2.add(UiBuilder.buildSwingComponent("MysqlFrame.usernameLabel", "JLabel", "<html><div style='width:30px; text-align:center;'>用户名</div></html>", labelPropMap), BorderLayout.WEST);
        subPane2.add(UiBuilder.buildSwingComponent("MysqlFrame.usernameField", "JTextField", textFieldPropMap), BorderLayout.EAST);
        contentPane.add(subPane2);
        
        JPanel subPane3 = new JPanel();
        subPane3.add(UiBuilder.buildSwingComponent("MysqlFrame.passwordLabel", "JLabel", "<html><div style='width:30px; text-align:center;'>密码</div></html>", labelPropMap), BorderLayout.WEST);
        subPane3.add(UiBuilder.buildSwingComponent("MysqlFrame.passwordField", "JTextField", textFieldPropMap), BorderLayout.EAST);
        contentPane.add(subPane3);
        
        JPanel subPane4 = new JPanel();
        subPane4.add(UiBuilder.buildSwingComponent("MysqlFrame.dbNameLabel", "JLabel", "<html><div style='width:30px; text-align:center;'>数据库名</div></html>", labelPropMap), BorderLayout.WEST);
        subPane4.add(UiBuilder.buildSwingComponent("MysqlFrame.dbNameField", "JTextField", textFieldPropMap), BorderLayout.EAST);
        contentPane.add(subPane4);
        add(contentPane);
        
        Box bottomPane = Box.createHorizontalBox();
//        bottomPane.setBorder(BorderFactory.createLineBorder(Color.RED));
        bottomPane.setAlignmentX(Component.CENTER_ALIGNMENT);
        bottomPane.setBounds(0, 3 * this.getHeight() / 4, this.getWidth(), this.getHeight() / 10);
        bottomPane.add(UiBuilder.buildSwingComponent("MysqlFrame.tryConnect", "JButton", "测试连接", event -> {
            JTextField hostField = (JTextField) UiBuilder.componentCacheMap.get("MysqlFrame.hostField");
            JTextField usernameField = (JTextField) UiBuilder.componentCacheMap.get("MysqlFrame.usernameField");
            JTextField passwordField = (JTextField) UiBuilder.componentCacheMap.get("MysqlFrame.passwordField");
            JTextField dbNameField = (JTextField) UiBuilder.componentCacheMap.get("MysqlFrame.dbNameField");
            if (UiBuilder.isBlank(hostField.getText()) || UiBuilder.isBlank(usernameField.getText()) || UiBuilder.isBlank(passwordField.getText()) || UiBuilder.isBlank(dbNameField.getText())) {
                JOptionPane.showMessageDialog(desktopPane.getParent(), "请填写Mysql连接信息", "警告", JOptionPane.WARNING_MESSAGE);
                return;
            }
            boolean connectSuccess = MysqlFetcher.tryConnect(hostField.getText() + "," + usernameField.getText() + "," + passwordField.getText() + "," + dbNameField.getText());
            if (connectSuccess) {
                JOptionPane.showMessageDialog(desktopPane.getParent(), "连接成功", "成功", JOptionPane.INFORMATION_MESSAGE);
            } else {
                JOptionPane.showMessageDialog(desktopPane.getParent(), "连接失败", "失败", JOptionPane.ERROR_MESSAGE);
            }
        }));
        bottomPane.add(UiBuilder.buildSwingComponent("MysqlFrame.ok", "JButton", "确定", event -> {
            JTextField hostField = (JTextField) UiBuilder.componentCacheMap.get("MysqlFrame.hostField");
            JTextField usernameField = (JTextField) UiBuilder.componentCacheMap.get("MysqlFrame.usernameField");
            JTextField passwordField = (JTextField) UiBuilder.componentCacheMap.get("MysqlFrame.passwordField");
            JTextField dbNameField = (JTextField) UiBuilder.componentCacheMap.get("MysqlFrame.dbNameField");
            if (UiBuilder.isBlank(hostField.getText()) || UiBuilder.isBlank(usernameField.getText()) || UiBuilder.isBlank(passwordField.getText()) || UiBuilder.isBlank(dbNameField.getText())) {
                JOptionPane.showMessageDialog(desktopPane.getParent(), "请填写Mysql连接信息", "警告", JOptionPane.WARNING_MESSAGE);
                return;
            }
            try (FileWriter fileWriter = new FileWriter(Main.tempFilePath + "mysql.conf")) {
                fileWriter.write(hostField.getText() + "," + usernameField.getText() + "," + passwordField.getText() + "," + dbNameField.getText());
                fileWriter.flush();
                fileWriter.close();
                
                JInternalFrame[] allFrames = desktopPane.getAllFrames();
                Optional<JInternalFrame> internalFrame = Arrays.stream(allFrames).filter(innerFrame -> innerFrame.getName().equals(MysqlFrame.NAME)).findFirst();
                internalFrame.ifPresent(JInternalFrame::dispose);
            } catch (Exception e) {
                e.printStackTrace();
                JOptionPane.showMessageDialog(desktopPane.getParent(), "Mysql配置保存失败", "错误", JOptionPane.ERROR_MESSAGE);
            }
        }));
        bottomPane.add(UiBuilder.buildSwingComponent("MysqlFrame.canel", "JButton", "取消", event -> {
            JInternalFrame[] allFrames = desktopPane.getAllFrames();
            Optional<JInternalFrame> internalFrame = Arrays.stream(allFrames).filter(innerFrame -> innerFrame.getName().equals(MysqlFrame.NAME)).findFirst();
            internalFrame.ifPresent(JInternalFrame::dispose);
        }));
        add(bottomPane);
        
        desktopPane.add(this);
    }
    
}
